﻿// ////////////////////////////////////////////////////////////////////////////
// 
// SWE Case Tool
// 
// Solution:	CaseTool
// Project:		2. CaseTool GUI
// Filename:	Settings.cs
// 
// File Created:	09:29 16.05.2013 
// Last Changed:	18:11 05.06.2013
// 
// ////////////////////////////////////////////////////////////////////////////

using System;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace SWE.CaseTool.GUI
{
	/// <summary>
	/// General settings. A (singleton) class, which loads itselfs from a predefined file. Predefined means
	/// at compiletime. It is considered as a singleton, but because we internally use the XmlSerializer, we
	/// need a public constructor with no parameter, which would allow multiple instances of this class.
	/// Altough the public construcor, mutliple instances could lead to unexpected behaviour.
	/// </summary>
	public class Settings
	{
		#region Singleton stuff

		private const string SETTINGS_FILE = @"ExternalFiles\settings.xml";

		private static Settings instance;

		/// <summary>
		/// Don't use this constructor, use GetInstance instead!.
		/// Settings is designed to be a singleton, which should have a private constructor.
		/// For simpler implementation we decided to use the XmlSerializer, which needs the
		/// constructor to be public!!.
		/// Initializes a new instance of the <see cref="Settings" /> class.
		/// </summary>
		public Settings()
		{
			MethodFolder =  @"..\..\Methods";
		}

		/// <summary>
		/// Returns or create the settings instance.
		/// </summary>
		/// <returns></returns>
		public static Settings GetInstance()
		{
			if (instance == null)
			{
				if (File.Exists(SETTINGS_FILE))
				{
					try
					{
						using (var stream = new StreamReader(SETTINGS_FILE))
						{
							instance = (Settings) new XmlSerializer(typeof (Settings)).Deserialize(stream);
						}
					}
					catch (Exception ex)
					{
						MessageBox.Show(@"Unable to load the settings file:" + ex.Message, @"Error: Unable to load the settings file");
						instance = new Settings();
					}
				}
				else
				{
					instance = new Settings();
				}
			}
			return instance;
		}

		/// <summary>
		/// Saves the instance to the settings file.
		/// </summary>
		public static void SaveInstance()
		{
			var temp = GetInstance(); // Must be called before, because if the instance does not exist it will
			// be created. But for the creation it needs the permission to read the 
			// SETTINGS_FILE which would be denied through the StreamWrite below.

			var path = Path.GetDirectoryName(SETTINGS_FILE);
			if (path != null && !Directory.Exists(path))
			{
				Directory.CreateDirectory(path);
			}
			
			using (var stream = new StreamWriter(SETTINGS_FILE))
			{
				new XmlSerializer(typeof(Settings)).Serialize(stream, temp);
			}
		}

		#endregion

		/// <summary>
		/// Gets or sets the folder which contains the methods.
		/// </summary>
		/// <value>
		/// The Method folder.
		/// </value>
		public string MethodFolder { set; get; }

		/// <summary>
		/// Gets or sets the path on which the project was saved last-
		/// </summary>
		/// <value>
		/// The last save path.
		/// </value>
		public string LastSavePath { set; get; }
	}
}